package summary;

/**
 * @Author: 海琳琦
 * @Date: 2022/3/7 10:57
 * https://leetcode-cn.com/problems/squares-of-a-sorted-array/
 */
public class Title977 {

    public int[] sortedSquares(int[] nums) {
        int n = nums.length;
        int[] result = new int[n];
        int left = 0, resultIndex = n - 1;
        for (int right = n - 1; right >= left; ) {
            if (-nums[left] > nums[right]) {
                result[resultIndex] = nums[left] * nums[left];
                left++;
            } else {
                result[resultIndex] = nums[right] * nums[right];
                right--;
            }
            resultIndex--;
        }
        return result;
    }








    public static int[] sortedSquares1(int[] nums) {
        int n = nums.length;
        int left = 0, right = n - 1;
        int resultIndex = n - 1;
        int[] result = new int[n];
        while (left <= right) {
            if (-nums[left] > nums[right]) {
                result[resultIndex] = nums[left] * nums[left];
                left++;
            }else{
                result[resultIndex] = nums[right] * nums[right];
                right--;
            }
            resultIndex--;
        }
        return result;
    }

    public static void main(String[] args) {
        int[] result = {-4, -1, 0, 3, 10};
        sortedSquares1(result);
    }
}
